package com.company.base.web.servlet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.company.base.utils.WeChatUtil;
import com.company.site.mode.token.AccessToken;

public class TokenThread implements Runnable{
    private static Logger log = LoggerFactory.getLogger(TokenThread.class);  
   
    public static String appid = "";  
  
    public static String appsecret = "";  
    public static AccessToken accessToken = null;  
  
    public void run() {  
        while (true) {  
            try {  
                accessToken = WeChatUtil.getAccessToken(appid, appsecret);  
                if (null != accessToken) {  
                	// log.info("access_tokentoken:{}", accessToken.getExpiresIn(), accessToken.getToken());  
                    Thread.sleep((accessToken.getExpiresIn() - 200) * 1000);  
                } else {  
                    //  
                    Thread.sleep(60 * 1000);  
                }  
            } catch (InterruptedException e) {  
                try {  
                    Thread.sleep(60 * 1000);  
                } catch (InterruptedException e1) {  
                    log.error("{}", e1);  
                }  
                log.error("{}", e);  
            }  
        }  
    } 
}
